package com.gorkr;

import java.util.*;

/**
 * @author gorkr
 * @title: L679
 * @projectName my-leetcode
 * @description: 24点游戏
 * @date 2021/12/1410:18
 *
 * 回溯解法？
 */
public class L679 {
    static final int TARGET = 24;
    static final double EPSILON = 1e-6;
    static final int ADD=0, MULTIPLY=1, SUBTRACT=2, DIVIDE=3;

    public List dealInput(){
        final Scanner scanner = new Scanner(System.in);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            String t = scanner.next();
            list.add(t);

        }
//
//        System.out.println("结束");
        List<Double> listDouble = new ArrayList<>();
        for (String item: list
             ) {
            if(Objects.equals(item, "A")){
                listDouble.add(1.0);
            }else if (Objects.equals(item,"J")){
                listDouble.add(11.0);
            }else if(Objects.equals(item,"Q")){
                listDouble.add(12.0);
            }else if(Objects.equals(item, "K")){
                listDouble.add(13.0);
            }else {
                listDouble.add(Double.parseDouble(item));
            }
        }
//        System.out.println("wocao?");
        return listDouble;
    }


    // 仅将int[] 转换为Double[]
    public boolean judgePoint24(int[] nums){
        List<Double> list = new ArrayList<>();
        for (int num :
                nums) {
            list.add((double) num);
        }
        return solve(list);
    }

    public boolean solve(List<Double> list){
        if(list.size() == 0){
            return false;
        }
        if(list.size() == 1){
            return Math.abs(list.get(0)-TARGET)<EPSILON;
        }

        // 遍历一层选择
        int size = list.size();
        for(int i=0; i<size; i++){
            for (int j = 0; j < size; j++) {
                if(i!=j){
                    ArrayList<Double> list2 = new ArrayList<>();
                    for (int k = 0; k < size; k++) {
                        if (k!=i && k!=j){
                            list2.add(list.get(k));
                        }
                    }
                    for (int k=0; k<4; k++){
                        if(k<2&& i>j){
                            continue;
                        }
                        if(k==ADD){
                            list2.add(list.get(i)+list.get(j));
                        } else if(k==MULTIPLY){
                            list2.add(list.get(i) * list.get(j));
                        } else if(k==SUBTRACT){
                            list2.add(list.get(i) - list.get(j));
                        }
                        else if(k == DIVIDE){
                            if(Math.abs(list.get(j))<EPSILON){
                                continue;
                            }else {
                                list2.add(list.get(i) / list.get(j));

                            }
                        }
                        if(solve(list2)){
                            return true;
                        }
                        // 撤销选择
                        list2.remove(list2.size()-1);
                    }
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        final L679 l679 = new L679();
        final List list = l679.dealInput();
//        for (int i = 0; i < list.size(); i++) {
//            System.out.println(list.get(i));
//
//        }
        if(l679.solve(list)){
            System.out.print(1);
        }else {
            System.out.print(0);
        }



    }
}
